﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SmartEP.Presenter;
using SmartEP.DTO;

namespace SmartDesktop
{
    public partial class MainForm : Form
    {
        #region Consts
        private const int PATIENT_CARE_INDEX = 1;
        private const int MEASUREMENT_INDEX = 2;
        private const int ORDER_INDEX = 3;
        private const int PATIENT_INDEX = 4;
        private const int CLAIM_INDEX = 5;
        private const int FRAME_STYLING_INDEX = 6;
        private const int TRYON_INDEX = 7;
        private const int VISION_TEST_INDEX = 8;
        private const int CONTACT_LENS_INDEX = 9;
        private const int CONFIGURATION_INDEX = 10;
        #endregion

        #region Fields
        private int currentIndexMainMenu = -1;
        private UserControl currentView;        
        private List<UserControl> views = new List<UserControl>();
        private PatientCarePresenter _patientCarePresenter;
        private DatabasePresenter _databasePresenter=new DatabasePresenter();
        #endregion

        public static User CurrentUser;
        public static ToolStripLabel BarCaption;


        public MainForm()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            MainForm.BarCaption = this.tlslWelcome;
            Initial();            
        }

        #region Private Methods
        private UserControl GetView(int idx)
        {
            UserControl uc = this.views[idx];

            if (uc == null)
            {
                switch (currentIndexMainMenu)
                {
                    case PATIENT_CARE_INDEX: // Patient Care
                        UserControl[] ucPatientCares = new UserControl[4];

                        CheckInView ci = new CheckInView(_patientCarePresenter);
                        ci.Dock = DockStyle.Fill;
                        ucPatientCares[0] = ci;

                        Step1View step = new Step1View(_patientCarePresenter);
                        step.Dock = DockStyle.Fill;
                        ucPatientCares[1] = step;

                        TodayPatientView today = new TodayPatientView(_patientCarePresenter);
                        today.Dock = DockStyle.Fill;
                        ucPatientCares[2] = today;

                        SearchPatientView search = new SearchPatientView(_patientCarePresenter);
                        search.Dock = DockStyle.Fill;
                        ucPatientCares[3] = search;

                        uc = ucPatientCares[idx];
                        break;
                    case MEASUREMENT_INDEX: // Measurement
                        UserControl[] ucMeasurementController = new UserControl[6];

                        TakePhoto tpt = new TakePhoto();
                        tpt.Dock = DockStyle.Fill;
                        ucMeasurementController[0] = tpt;

                        MeasurementPages mp = new MeasurementPages();
                        mp.Dock = DockStyle.Fill;
                        ucMeasurementController[1] = mp;
                        ucMeasurementController[2] = mp;
                        ucMeasurementController[3] = mp;
                        ucMeasurementController[4] = mp;
                        ucMeasurementController[5] = mp;

                        uc = ucMeasurementController[idx];
                        break;
                    case ORDER_INDEX: // Orders
                        UserControl[] ucOrders = new UserControl[5];

                        ViewOrders vos = new ViewOrders();
                        vos.Dock = DockStyle.Fill;
                        ucOrders[0] = vos;

                        PrivateOrder po = new PrivateOrder();
                        po.Dock = DockStyle.Fill;
                        ucOrders[1] = po;

                        InsuranceOrder io = new InsuranceOrder();
                        io.Dock = DockStyle.Fill;
                        ucOrders[2] = io;

                        FrameSelect ceo = new FrameSelect();
                        ceo.Dock = DockStyle.Fill;
                        ucOrders[3] = ceo;

                        ContactLensOrder clo = new ContactLensOrder();
                        clo.Dock = DockStyle.Fill;
                        ucOrders[4] = clo;

                        uc = ucOrders[idx];
                        break;
                    case PATIENT_INDEX: // Patients
                        UserControl[] ucPatients = new UserControl[2];

                        ViewPatients vps = new ViewPatients();
                        vps.Dock = DockStyle.Fill;
                        ucPatients[0] = vps;

                        PatientValidation pv = new PatientValidation();
                        pv.Dock = DockStyle.Fill;
                        ucPatients[1] = pv;

                        uc = ucPatients[idx];
                        break;
                    case CLAIM_INDEX: // Claims 
                        UserControl[] ucClaims = new UserControl[2];

                        ViewClaims vcs = new ViewClaims();
                        vcs.Dock = DockStyle.Fill;
                        ucClaims[0] = vcs;

                        PatientValidationSubmitClaim pvs = new PatientValidationSubmitClaim();
                        pvs.Dock = DockStyle.Fill;
                        ucClaims[1] = pvs;

                        uc = ucClaims[idx];
                        break;
                    case FRAME_STYLING_INDEX: // Fame Styling 
                        UserControl[] ucFrameStyling = new UserControl[4];

                        FrameBrands fb = new FrameBrands();
                        fb.Dock = DockStyle.Fill;
                        ucFrameStyling[0] = fb;

                        FrameFavorite ff = new FrameFavorite();
                        ff.Dock = DockStyle.Fill;
                        ucFrameStyling[1] = ff;

                        FrameCompare fc = new FrameCompare();
                        fc.Dock = DockStyle.Fill;
                        ucFrameStyling[2] = fc;

                        QuickSearch qs = new QuickSearch();
                        qs.Dock = DockStyle.Fill;
                        ucFrameStyling[3] = qs;

                        uc = ucFrameStyling[idx];
                        break;
                    case TRYON_INDEX: // Try On
                        UserControl[] ucTryOn = new UserControl[6];

                        TakePicture tp = new TakePicture();
                        tp.Dock = DockStyle.Fill;
                        ucTryOn[0] = tp;

                        UploadPhoto up = new UploadPhoto();
                        up.Dock = DockStyle.Fill;
                        ucTryOn[1] = up;

                        SelectPicture sp = new SelectPicture();
                        sp.Dock = DockStyle.Fill;
                        ucTryOn[2] = sp;

                        TryOn to = new TryOn();
                        to.Dock = DockStyle.Fill;
                        ucTryOn[3] = to;

                        SelectFrame sf = new SelectFrame();
                        sf.Dock = DockStyle.Fill;
                        ucTryOn[4] = sf;

                        AddFrame af = new AddFrame();
                        af.Dock = DockStyle.Fill;
                        ucTryOn[5] = af;

                        uc = ucTryOn[idx];
                        break;
                    case VISION_TEST_INDEX: // Visiion Test                        
                        break;
                    case CONTACT_LENS_INDEX: // Contact Lens 
                        ContactLensMaster tol = new ContactLensMaster();
                        tol.Dock = DockStyle.Fill;
                        #region SerDayPerLens
                        switch (idx)
                        {
                            case 0:
                                tol.SerDayPerLens = "1"; tol.Name = "Disposable Daily";
                                break;
                            case 1:
                                tol.SerDayPerLens = "7"; tol.Name = "Disposable  Weekly";
                                break;
                            case 2:
                                tol.SerDayPerLens = "30"; tol.Name = "Disposable Monthly";
                                break;
                            case 3:
                                tol.SerDayPerLens = "180"; tol.Name = "Color Lenses";
                                break;
                            case 4:
                                tol.SerDayPerLens = "multi"; tol.Name = "Bifocal | Multifocal";
                                break;
                            case 5:
                                tol.SerDayPerLens = "90"; tol.Name = "Astigmatlsm Toric";
                                break;
                        }
                        #endregion
                        uc = tol;
                        break;
                }

            }
            return uc;
        }
        private void SetView(int idx)
        {
            UserControl uc = this.GetView(idx);

            if (uc != this.currentView)
            {
                if (uc != null)
                {
                    pnlContent.Controls.Add(uc);
                    if (!string.IsNullOrEmpty(uc.Name))
                        MainForm.BarCaption.Text = uc.Name;
                }

                if (this.currentView != null)
                    pnlContent.Controls.Remove(this.currentView);

                this.currentView = uc;
            }
        }
        private ToolStripButton CreateToolStripButton(Image imageMenuLeft, string tooltipText)
        {
            ToolStripButton tlsButton = new ToolStripButton();
            tlsButton.AutoSize = false;
            tlsButton.Width = 102; tlsButton.Height = 90;
            tlsButton.BackgroundImage = imageMenuLeft; tlsButton.BackgroundImageLayout = ImageLayout.Stretch;
            tlsButton.ToolTipText = tooltipText;
            views.Add(null);
            tlsButton.Click += new EventHandler(tlsLeftMenu_ItemClicked);

            return tlsButton;
        }
        private void Initial()
        {
            foreach (ToolStripItem tsi in this.tlsMainMenu.Items)
            {
                tsi.Click += new EventHandler(tlsMainMenu_ItemClicked);
            }
            ShowWelcome(true);
            InitUser();
        }
        private void SetButtonState(ToolStrip tls, int SelectedIndex)
        {
            int cnt = 0;
            foreach (ToolStripItem tlsItem in tls.Items)
            {
                if (tlsItem is ToolStripButton)
                {
                    ToolStripButton tlsButton = (ToolStripButton)tlsItem;
                    tlsButton.Checked = (cnt == SelectedIndex);
                }
                cnt++;
            }
        }
        private void ShowWelcome(bool status)
        {
            tlsLeftMenu.AutoSize = false;
            tlsLeftMenu.Visible = !status;
            pnlContent.Visible = !status;
        }
        private void LoadDatabaseInfo(string FileName)
        {
            System.Collections.ArrayList arr = _databasePresenter.ReadDatabaseXML(FileName);

            if (arr != null)
            {
                if (SmartEP.Model.DBConnection.IsConnectDatabase(Convert.ToString(arr[0]), Convert.ToString(arr[1]), Convert.ToString(arr[2]), Convert.ToString(arr[3])))
                {
                    return;
                }
            }

            MessageBox.Show("Connected to database fail... Please try agian!");
            ConnectDatabaseForm frm = new ConnectDatabaseForm();
            frm.ShowDialog();

        }
        #endregion

        #region Events
        private void tlsMainMenu_ItemClicked(object sender, EventArgs e)
        {
            ToolStripItem tsi = (ToolStripItem)sender;
            int idx = tsi.GetCurrentParent().Items.IndexOf(tsi);
            if (idx == currentIndexMainMenu || idx == 0) return;

            currentIndexMainMenu = idx;

            ShowWelcome(false);
            tlsLeftMenu.Items.Clear();
            tlsLeftMenu.Width = 106;
            pnlContent.Controls.Clear();

            switch (idx)
            {
                case PATIENT_CARE_INDEX://Patient care
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Patient_Check_in, "Patient CheckIn"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Patient_Registration, "Patient Registration"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Todays_Patient, "Today's Patient"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Search_Patient, "Search Patient"));

                    if (_patientCarePresenter == null)
                    {
                        _patientCarePresenter = new PatientCarePresenter(pnlContent);
                        _patientCarePresenter.RegistrationStepCompleted += RegistrationStepCompleted;
                        _patientCarePresenter.BackToPreviousRegistrationStep += BackToPreviousRegistrationStep;
                    }

                    break;
                case MEASUREMENT_INDEX://Measurement
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Take_A_Picture, "Take Picture"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Dist_PD, "Dist PD"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Near_PD, "Near PD"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Pantoscopic, "Pantoscopic"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Frame_Wrap, "Frame Wrap"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Vertex, "Vertex"));
                    break;
                case ORDER_INDEX://orders
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.View_Orders, "View Order"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Private_Order, "Private Order"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Insurance_Order, "Insurance Order"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Complete_Eyewear, "CompleteEyewear Order"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.ContactLensOrder, "ContactLens Order"));
                    break;
                case PATIENT_INDEX://patients
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.View_Patients, "View Patient"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.NewPatientSearch, "New Patient Search"));
                    break;
                case CLAIM_INDEX://Claims 
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.View_Claims, "View Claim"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Submit_Claim, "Submit Claim"));
                    break;
                case FRAME_STYLING_INDEX://FameStyling 
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.View_Catalog, "View Catalog"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.My_Favorites, "My Favorites"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Compare_Frames, "Compare Frame"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Quick_Search, "Quit Search"));
                    break;
                case TRYON_INDEX://try on
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Take_Picture, "Take a picture"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Upload_Photo, "Upload Photo"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Select_Picture, "Select Picture"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Try_On, "Try on"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.SelectFrame, "Select Frame"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Add_Frame, "Add Frame"));
                    break;
                case VISION_TEST_INDEX://visiion test 
                    tlsLeftMenu.Visible = false;
                    tlsLeftMenu.AutoSize = false;
                    tlsLeftMenu.Width = 0;
                    MainForm.BarCaption.Text = "Vision Test";
                    VisionTest vt = new VisionTest();
                    vt.Dock = DockStyle.Fill;
                    pnlContent.Controls.Add(vt);
                    break;
                case CONTACT_LENS_INDEX://Contact Lens 
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Monthly, "Disposable Daily"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Weekly, "Disposable Weekly"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Monthly, "Disposable Monthly"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Color, "Color Lenses"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Bi_MultiFocal, "Bifocal | Multifocal"));
                    tlsLeftMenu.Items.Add(CreateToolStripButton(Properties.Resources.Astigmatism, "Astigmatlsm Toric"));
                    break;
                default:
                    ShowWelcome(true);
                    break;
            }

            SetButtonState(tlsMainMenu, idx);

            if (tlsLeftMenu.Items.Count > 0)
            {
                this.SetView(0);
            }
        }
        private void tlsLeftMenu_ItemClicked(object sender, EventArgs e)
        {
            ToolStripItem tsi = (ToolStripItem)sender;
            int idx = tsi.GetCurrentParent().Items.IndexOf(tsi);
            this.SetView(idx);
            SetButtonState(tlsLeftMenu, idx);
        }
        private void tlsWelcom_Paint(object sender, PaintEventArgs e)
        {
            ToolStripLabel tlsLable = (ToolStripLabel)sender;
            tlsLable.Width = tlsLable.GetCurrentParent().Width - 30;
        }
        private void tlsExitMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void MainForm_Load(object sender, EventArgs e)
        {
            EnableItemMainMenu();
            pnlWelcomeMainForm.BackgroundImage = Properties.Resources.Welcome_Screen_01; 
            LoadDatabaseInfo("Database.xml");                       
            LoginUser();
        }
       
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
        private void RegistrationStepCompleted(int step)
        {
            switch (step)
            {
                case 1:
                    _patientCarePresenter.DisplayView(new Step2View(_patientCarePresenter));
                    break;
                case 2:
                    _patientCarePresenter.DisplayView(new Step3View(_patientCarePresenter));
                    break;
                case 3:
                    _patientCarePresenter.DisplayView(new Step4View(_patientCarePresenter));
                    break;
                case 4:
                    _patientCarePresenter.DisplayView(new Step5View(_patientCarePresenter));
                    break;
                case 5:
                    _patientCarePresenter.DisplayView(new Step6View(_patientCarePresenter));
                    break;
                case 6:
                    _patientCarePresenter.DisplayView(new Step7View(_patientCarePresenter));
                    break;
                case 7:
                    _patientCarePresenter.DisplayView(new Step8View(_patientCarePresenter));
                    break;
                case 8:
                    _patientCarePresenter.DisplayView(new Step9View(_patientCarePresenter));
                    break;
                case 9:
                    _patientCarePresenter.DisplayView(new Step10View(_patientCarePresenter));
                    break;
                case 10:
                    _patientCarePresenter.DisplayView(new Step11View(_patientCarePresenter));
                    break;
                case 11:
                    _patientCarePresenter.DisplayView(new Step12View(_patientCarePresenter));
                    break;
            }
        }
        private void BackToPreviousRegistrationStep(int step)
        {
            switch (step)
            {
                case 2:
                    _patientCarePresenter.DisplayView(new Step1View(_patientCarePresenter));
                    break;
                case 3:
                    _patientCarePresenter.DisplayView(new Step2View(_patientCarePresenter));
                    break;
                case 4:
                    _patientCarePresenter.DisplayView(new Step3View(_patientCarePresenter));
                    break;
                case 5:
                    _patientCarePresenter.DisplayView(new Step4View(_patientCarePresenter));
                    break;
                case 6:
                    _patientCarePresenter.DisplayView(new Step5View(_patientCarePresenter));
                    break;
                case 7:
                    _patientCarePresenter.DisplayView(new Step6View(_patientCarePresenter));
                    break;
                case 8:
                    _patientCarePresenter.DisplayView(new Step7View(_patientCarePresenter));
                    break;
                case 9:
                    _patientCarePresenter.DisplayView(new Step8View(_patientCarePresenter));
                    break;
                case 10:
                    _patientCarePresenter.DisplayView(new Step9View(_patientCarePresenter));
                    break;
                case 11:
                    _patientCarePresenter.DisplayView(new Step10View(_patientCarePresenter));
                    break;
            }
        }
		private void tlsLoginMenuItem_Click(object sender, EventArgs e)
        {
            LoginUser();
        }

        private void LoginUser()
        {
            LoginForm frm = new LoginForm();
            frm.ShowDialog();
            frm.Dispose();

            EnableItemMainMenu();
        }

        private void EnableItemMainMenu()
        {
            tsbPatientcare.Enabled = CurrentUser.CorrectPassword;
            tsbMeasurement.Enabled = CurrentUser.CorrectPassword;
            tsbOrderinfo.Enabled = CurrentUser.CorrectPassword;
            tsbPatientinfo.Enabled = CurrentUser.CorrectPassword;
            tsbClaiminfo.Enabled = CurrentUser.CorrectPassword;
            tsbFramestyling.Enabled = CurrentUser.CorrectPassword;
            tsbVirtualtryon.Enabled = CurrentUser.CorrectPassword;
            tsbVisiontest.Enabled = CurrentUser.CorrectPassword;
            tsbContactlens.Enabled = CurrentUser.CorrectPassword;
            tsbConfiguration.Enabled = CurrentUser.CorrectPassword;

            tlsLoginMenuItem.Enabled = !CurrentUser.CorrectPassword;
            tlsLogOutMenuItem.Enabled = CurrentUser.CorrectPassword;
        }

        private void tlsLogOutMenuItem_Click(object sender, EventArgs e)
        {
            InitUser();
            EnableItemMainMenu();
        }
        private void InitUser()
        {
            CurrentUser = new User();
        }
        #endregion
    }
}
